<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\AuthController;
use Org\Util\String;

Vendor('Filehuancun.Filehuancun');
Vendor('ExcleInOut.ExcleInOut');//Vendor 引入ExcleInOut目录下的ExcleInOut命名空间
class DdtjController extends Controller {
	public function index() {		
							
		$this->display();//echo 输出和assign要在这个这个函数之前设置完成。
	}
	public function tjajaxsicp()
	{
		$temp2 = explode(',',$_POST["arr2"]);
		$temp3 = explode(',',$_POST["arr3"]);
		$temp4 = explode(',',$_POST["arr4"]);
		//var_dump($temp2);//14
		//var_dump($temp3);//8
		//var_dump($temp4);
		//订单编码表数组
		$ddbmsz="";
		$ddbmszjs=0;
		for($ii=0;$ii<count($temp2);$ii=$ii+14)
		{
			$ddbmsz[$ddbmszjs]=$temp2[$ii+2];
			$ddbmszjs=$ddbmszjs+1;
		}
		//var_dump($ddbmsz);
		//去除重复项
		$arrddbmaa=  array_unique ( $ddbmsz );
		sort ( $arrddbmaa );
		
		//统计数组
		$tjsz="";
		//给tjsz 追加门店地址 配送类型。
		for($aa=0;$aa<count($arrddbmaa);$aa=$aa+1)
		{		
			$sel=0;
			for($ii=0;$ii<count($temp2);$ii=$ii+14)
			{
					
				if($arrddbmaa[$aa]==$temp2[$ii+2]){										
					$tjsz[$aa]["mddz"]=$temp2[$ii+5];				
					$tjsz[$aa]["pslx"]=$temp4[$sel];					
				}
				$sel=$sel+1;
			}			
		}
		
		
		//计算总发货最小件数和折合件数				
		for($aa=0;$aa<count($arrddbmaa);$aa=$aa+1)
		{
			$mdbhjs=0;			
			$tdjs=0;
			$zzhxs=0;
			for($ii=0;$ii<count($temp2);$ii=$ii+14)
			{
				 
				if($arrddbmaa[$aa]==$temp2[$ii+2]){
			
					$tjsz[$aa]["ddbm"]=$temp2[$ii+2];
					$tjsz[$aa]["md"]=$temp2[$ii+4];
					
					$mdbhjs=$mdbhjs+$temp3[$tdjs+6];
					//找到td件数除以th箱规。
					$zzhxs=$zzhxs+(double)$temp3[$tdjs+6]/(double)$temp2[$ii+12];
				}
				$tdjs=$tdjs+8;
			}
			$tjsz[$aa]["mdbhjs"]=(String)$mdbhjs;
			$tjsz[$aa]["zzhxs"]=(String)$zzhxs;
		}
		
		//var_dump($tjsz);
		session ('bhtj' , $tjsz);
		sleep(2);
	 }
	 public function tjajaxsicpdytj()
	 {
	 	 $dayinyl=session ('biaoti' );

	 	 var_dump($dayinyl);
	 	 //门店名+订单号数组
	 	 $mdddh="";	 	 
	 	 for($ii=0;$ii<count($dayinyl);$ii=$ii+1)
	 	 {
	 	 	$mdddh[$ii]=$dayinyl[$ii]["etddbm"]."+".$dayinyl[$ii]["khmd"];	 	 	
	 	 }
	 	 //去除重复项
	 	 $arrddbmaa=  array_unique ( $mdddh );
	 	 sort ( $arrddbmaa );
	 	 //按加号分隔数据
	 	 $arrt1="";
	 	 for($aa=0;$aa<count($arrddbmaa);$aa=$aa+1)
	 	 {
	 	 	$temp = explode('+',$arrddbmaa[$aa]);
	 	 	$arrt1[$aa]["ddbm"]=$temp[0];
	 	 	$arrt1[$aa]["md"]=$temp[1];
	 	 }
	 	 
	 	 for($aa=0;$aa<count($arrt1);$aa=$aa+1)
	 	 {
	 	 	//配置 其他属性。
	 	 	for($ii=0;$ii<count($dayinyl);$ii=$ii+1)
	 	 	{
	 	 		 
	 	 		if($arrt1[$aa]["ddbm"]==$dayinyl[$ii]["etddbm"]){

	 	 		 	$arrt1[$aa]["pslx"]=$dayinyl[$ii]["pslx"];
	 	 		 	$arrt1[$aa]["mddz"]=$dayinyl[$ii]["mddz"];
	 	 		 	$arrt1[$aa]["xianggui"]=$dayinyl[$ii]["xianggui"];
	 	 		 	
	 	 		 	break;
	 	 		}
	 	 	
	 	 	}
	 	 	//最小件数合计
	 	 	$mdbhjs=0;
	 	 	//折合箱数合计	 
	 	 	$zzhxs=0;	 		
	 	 	for($ii=0;$ii<count($dayinyl);$ii=$ii+1)
	 	 	{
	 	 			
	 	 		if($arrt1[$aa]["ddbm"]==$dayinyl[$ii]["etddbm"]){
	 	 			$mdbhjs=$mdbhjs+$dayinyl[$ii]["shjs"];
	 	 			$zzhxs=$zzhxs+(double)$dayinyl[$ii]["shjs"]/(double)$dayinyl[$ii]["xianggui"];
	 	 		}
	 	 		
	 	 	}
	 	 	$arrt1[$aa]["mdbhjs"]=(String)$mdbhjs;
	 	 	$arrt1[$aa]["zzhxs"]=(String)$zzhxs;
	 	 }
	 	//var_dump($arrt1);
	 	session ('bhtj' , $arrt1);
	 	sleep(2);
	 }
	public function ajaxqrdd()//ajax测试方法
	{
		//拿到选中订单号后可以直接写入 订单打印 标记。
		$temp2 = $_POST["arr2"];//订单号
		$temp4 = $_POST["user"];//当前操作员
		$temp3 = explode(',',$temp2);
		
		$sjkTemp = session ('biaoti');
				
		$a1arr=array();
		$idjs=0;
		//拿到虽有已选中订单的 id值。
		for($ii = 0; $ii < count ( $temp3 ); $ii ++) {
			for($i = 0; $i < count ( $sjkTemp ); $i ++) {
				if($temp3[$ii]==$sjkTemp [$i] [etddbm]){
					$a1arr[$idjs][id]=$sjkTemp [$i] [id];
					$idjs=$idjs+1;
				}
		
			}
		}
		print_r($a1arr);
		
		$user = M('cmxmckb');
		$today  =  date ( "Y-m-d H:i:s" );
	
		
		for ($i=0;$i<count($a1arr);$i++){
		$map['id'] =$a1arr[$i][id];
		$idzhi=$user->where($map)->select();//后期这里要按照条件筛选
		
		
		$data['id'] =$a1arr[$i][id];
		$data['dybj'] =$idzhi[0][dybj]+1;
		$data ['dyjtsj'] = $today;
		$data ['dyczy'] = $temp4;
		$user->save($data);
		}
		////////////////////////////////////////////////////////////////////////
		session ('xzddsj' , $temp2);
		sleep(2);
		
	
	}
	 
	 public function tjajaxSc()
	 {
	 	
	 	
	 	$temp1 = $_POST["arr1"];
	 	$temp2 = explode(',',$_POST["arr2"]);;
	 
	 	//var_dump($temp1) ;
	 	//生成配送类型+单品的数组。
	 	$arrtemp="";
	 	$arrtempjs=0;
	 	for($ii = 0; $ii < count ( $temp2 ); $ii =$ii+3) {
	 		$arrtemp[$arrtempjs]=$temp2[$ii]."+".$temp2[$ii+1];		 		
	 		$arrtempjs=$arrtempjs+1;
	 	}
	 
	 	$arrddbmaa=  array_unique ( $arrtemp );	 
	 	sort ( $arrddbmaa );
	 	//var_dump($arrddbmaa) ;
	 	//分离配送类型+单品的数组。
	 	$lxpinming="";
	
	 	for($ii = 0; $ii < count ( $arrddbmaa ); $ii =$ii+1) {
	 		//取得当前值，分割为数组。
	 		$ltemp=explode('+',$arrddbmaa[$ii]);
	 		$lxpinming[$ii]["leixing"]=$ltemp[0];
	 		$lxpinming[$ii]["pinming"]=$ltemp[1];
	 	}
	 	//var_dump($lxpinming) ;
	 	//重新计算配送类型+单品的总件数。
	 	for($ii = 0; $ii < count ( $lxpinming ); $ii =$ii+1) {
	 		$tempjs=0;
	 		for($aa = 0; $aa < count ( $temp2 ); $aa =$aa+3) {
	 			 	if($lxpinming[$ii]["leixing"]==$temp2[$aa]&&$lxpinming[$ii]["pinming"]==$temp2[$aa+1]){
	 			 		$tempjs=$tempjs+$temp2[$aa+2];
	 			 	}	
	 		 
	 	   }
	 	   //配送类型+品名的 总件数
	 	   $lxpinming[$ii]["zjs"]=$tempjs;
	 	}
	 	//var_dump($lxpinming) ;
	 	//var_dump($temp1) ;
	 	
	 	//报货数据分析。
	 	$bhsjfx="";
	 	//报货数据分析计数 按实际无上限
	 	$bhsjfxjs=0;
	 	for($ii = 0; $ii < count ( $lxpinming ); $ii =$ii+1) {
	 		//报货数大于当前行库存数时使用，保存当前报货总数		
	 		$kcyus=$lxpinming[$ii]["zjs"];
	 		for($aa = 0; $aa < count ( $temp1 ); $aa =$aa+1) {
	 			
	 			if($lxpinming[$ii]["pinming"]==$temp1[$aa]["pinming"]){
	 				//如果报货总件数少于当前批次库存则直接使用当前批次。
	 				
	 				if($kcyus<=(double)$temp1[$aa]["zskc"]&&(double)$temp1[$aa]["zskc"]>0){	 				
	 					 
	 					 $bhsjfx[$bhsjfxjs]["leixing"]=$lxpinming[$ii]["leixing"];
	 					 $bhsjfx[$bhsjfxjs]["pinming"]=$lxpinming[$ii]["pinming"];
	 					 $bhsjfx[$bhsjfxjs]["zjs"]=$kcyus;
	 					 $bhsjfx[$bhsjfxjs]["pici"]=$temp1[$aa]["pici"];
	 					 $bhsjfx[$bhsjfxjs]["hwlx"]=$temp1[$aa]["hwlx"];
	 					 $bhsjfx[$bhsjfxjs]["zskc"]=$temp1[$aa]["zskc"];
	 					 $bhsjfx[$bhsjfxjs]["zskcjc"]=(string)((double)$temp1[$aa]["zskc"]-(double)$bhsjfx[$bhsjfxjs]["zjs"]);
	 					 $bhsjfx[$bhsjfxjs]["xnkc"]=$temp1[$aa]["xnkc"];
	 					 $bhsjfx[$bhsjfxjs]["xnkcjc"]=(string)((double)$temp1[$aa]["xnkc"]-(double)$bhsjfx[$bhsjfxjs]["zjs"]);
	 					 $bhsjfx[$bhsjfxjs]["kwhb"]=$temp1[$aa]["kwhb"];
	 					 $kcyus=0;
	 					 $bhsjfxjs=$bhsjfxjs+1;
	 					 //如果$kcyus 等于0 退出当前循环，表示报货数目已经匹配。
	 					 break;
	 				}
	 				//现有报货数大于实际库存，且实际库存不等于零。
	 				if($kcyus>(double)$temp1[$aa]["zskc"]&&(double)$temp1[$aa]["zskc"]>0){
	 				
	 					$bhsjfx[$bhsjfxjs]["leixing"]=$lxpinming[$ii]["leixing"];
	 					$bhsjfx[$bhsjfxjs]["pinming"]=$lxpinming[$ii]["pinming"];
	 					$bhsjfx[$bhsjfxjs]["zjs"]=$temp1[$aa]["zskc"];
	 					$bhsjfx[$bhsjfxjs]["pici"]=$temp1[$aa]["pici"];
	 					$bhsjfx[$bhsjfxjs]["hwlx"]=$temp1[$aa]["hwlx"];
	 					$bhsjfx[$bhsjfxjs]["zskc"]=$temp1[$aa]["zskc"];
	 					$bhsjfx[$bhsjfxjs]["zskcjc"]=(string)((double)$temp1[$aa]["zskc"]-(double)$bhsjfx[$bhsjfxjs]["zjs"]);
	 					$bhsjfx[$bhsjfxjs]["xnkc"]=$temp1[$aa]["xnkc"];
	 					$bhsjfx[$bhsjfxjs]["xnkcjc"]=(string)((double)$temp1[$aa]["xnkc"]-(double)$bhsjfx[$bhsjfxjs]["zjs"]);
	 					$bhsjfx[$bhsjfxjs]["kwhb"]=$temp1[$aa]["kwhb"];
	 					$kcyus=$kcyus-(double)$temp1[$aa]["zskc"];
	 					$bhsjfxjs=$bhsjfxjs+1;
	 					//echo $bhsjfx[$bhsjfxjs]["pinming"].$kcyus."<>";
	 				}
	 			}
	 		 	
	 		}
	 		//如果最后还有单品没有匹配则 表示无库存
	 		if($kcyus>0){
	 		
	 			$bhsjfx[$bhsjfxjs]["leixing"]=$lxpinming[$ii]["leixing"];
	 			$bhsjfx[$bhsjfxjs]["pinming"]=$lxpinming[$ii]["pinming"];
	 			$bhsjfx[$bhsjfxjs]["zjs"]=(string)("-".$kcyus);
	 			$bhsjfx[$bhsjfxjs]["pici"]="";
	 			$bhsjfx[$bhsjfxjs]["hwlx"]="";
	 			$bhsjfx[$bhsjfxjs]["zskc"]="";
	 			$bhsjfx[$bhsjfxjs]["zskcjc"]="";
	 			$bhsjfx[$bhsjfxjs]["xnkc"]="";
	 			$bhsjfx[$bhsjfxjs]["xnkcjc"]="";
	 			$bhsjfx[$bhsjfxjs]["kwhb"]="";	 			
	 			$bhsjfxjs=$bhsjfxjs+1;
	 			
	 		}
	 		
	 	}
	 	
	 	session ('bhsjfx' , $bhsjfx);
	 	sleep(2);
	 	//var_dump($bhsjfx) ;
	 	//  json_encode ( $a1arr ,JSON_UNESCAPED_UNICODE ).'fenge'.json_encode ( session ('biaoti') ,JSON_UNESCAPED_UNICODE );
	 }
	 public function tjajaxceshi()
	 {
	 	echo json_encode ( session ('bhtj'),JSON_UNESCAPED_UNICODE );
	 	session ('bhtj' , null);
	 }

	
}

